﻿
using AutoMapper;
using NFine.Data.Dto.SystemManage;
using NFine.Data.Entity.SystemManage;
using NFine.Repository.SystemManage;
using NFine.Util;
using SharpRepository.Repository.Queries;
using SharpRepository.Repository.Specifications;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NFine.Service.SystemManage
{
    public class DictService
    {
        DictRepository dictRepository = new DictRepository(SharpRepoConfig.sharpRepoConfig, "efCore");
        DictItemRepository dictItemRepository = new DictItemRepository(SharpRepoConfig.sharpRepoConfig, "efCore");

        public List<DictGridDto> GetList()
        {
            long? tenantId = OperatorProvider.Provider.GetOperator().TenantModel.Id;
            var specification = new Specification<Dict>(t => t.TenantId == tenantId);
            var sortingOtopns = new SortingOptions<Dict, int?>(x => x.SortCode, isDescending: false);
            var list = dictRepository.FindAll(specification, sortingOtopns).ToList();
            return Mapper.Map<List<DictGridDto>>(list);
        }

        public DictOutputDto GetForm(string keyValue)
        {
            long? tenantId = OperatorProvider.Provider.GetOperator().TenantModel.Id;
            long id = Convert.ToInt64(keyValue);
            DictOutputDto dictOutputDto = new DictOutputDto();
            Dict dict = dictRepository.Find(t => t.TenantId == tenantId && t.Id == id);
            AutoMapper.Mapper.Map<Dict, DictOutputDto>(dict, dictOutputDto);
            return dictOutputDto;
        }

        public void SubmitForm(DictInputDto dictInputDto, string keyValue)
        {
            long? tenantId = OperatorProvider.Provider.GetOperator().TenantModel.Id;
            long userId = OperatorProvider.Provider.GetOperator().Id;
            Dict dict = new Dict();
            if (!string.IsNullOrEmpty(keyValue))
            {
                long id = long.Parse(keyValue);
                dict = dictRepository.Find(t => t.TenantId == tenantId && t.Id == id);
                AutoMapper.Mapper.Map<DictInputDto, Dict>(dictInputDto, dict);
                dict.ModifierUserId = userId;
                dict.ModifiedTime = DateTime.Now;
                dictRepository.Update(dict);
            }
            else
            {
                AutoMapper.Mapper.Map<DictInputDto, Dict>(dictInputDto, dict);
                dict.Id = IdWorkerHelper.GenId64();
                dict.DeletedMark = false;
                dict.EnabledMark = true;
                dict.TenantId = tenantId;
                dict.CreatorUserId = userId;
                dict.CreatedTime = DateTime.Now;
                dictRepository.Add(dict);
            }
        }

        public List<DictItemGridDto> GetDictItemList(string enCode)
        {
            long? tenantId = OperatorProvider.Provider.GetOperator().TenantModel.Id;
            var dict = dictRepository.Find(t => t.TenantId == tenantId && t.EnCode == enCode);
            var dictItems = dictItemRepository.FindAll(t =>t.Dict.TenantId == tenantId && t.DictId == dict.Id);
            return Mapper.Map<List<DictItemGridDto>>(dictItems);
        }
    }
}
